import { Dao, SQL, SQLiteDatabase, SqlReturnTypes } from '@zxhhyj/storm';
import { songTable } from '../model/Song';

const queryAlbums = SQL
  .select(
    songTable.coverUrl.as('albumCover'),
    songTable.albumName.as('albumName'),
    songTable.songName.as('songName'))
  .form(songTable)
  .where()
  .isNoNull(songTable.albumName)
  .groupBy(songTable.albumName)

export type Album = SqlReturnTypes<typeof queryAlbums>

export class MediaLibDao extends Dao {
  private readonly db = SQLiteDatabase.form(this)

  getAlbums(): Promise<Album[]> {
    return this.db.querySql(queryAlbums)
  }

  getSongs() {
    return this.db.query(songTable)
  }

  deleteSong(id: number) {
    return this.db.deleteIf(songTable, it => it.equalTo(songTable.id, id))
  }
}